package com.fyzk.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.data.preference.MySharedPreferences;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.regex.Pattern;

import http.Common.CommandUtil;
import http.DataUtil.text.TextUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/6/7.
 */
@ContentView(R.layout.ac_change_nickname)
public class ChangeNickname extends BaseActivity {

    @ViewInject(R.id.changenickname_topview)
    private TopTitleView topView;

    @ViewInject(R.id.changenickname_nickname_et)
    private EditText nickname_et;

    @ViewInject(R.id.changenickname_tishi_tv)
    private TextView tishi_tv;

    @ViewInject(R.id.changenickname_sure_tv)
    private TextView sure_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        topView.setOnTitleClickListener(this);

        tishi_tv.setVisibility(View.GONE);
        if (!MySharedPreferences.getInstance().getAlias().equals("")) {
            nickname_et.setText(MySharedPreferences.getInstance().getAlias());
        }

        sure_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (!MySharedPreferences.getInstance().getAlias().equals("")) {
//                    AppToast.getToast().show("您已修改过昵称");
//                    return;
//                }

                if (TextUtil.isEmpty(nickname_et.getText().toString())) {
                    AppToast.getToast().show("请输入新昵称");
                    return;
                }

//                if (Pattern.matches("[0-9]{4,16}$", nickname_et.getText().toString())) {
//                    nickname_et.setError("昵称不能是纯数字！");
//                    showToast("昵称不能是纯数字！");
//                    nickname_et.requestFocus();
//                    nickname_et.setSelectAllOnFocus(true);
//                    return;
//                }
//                if (!Pattern.matches("[a-zA-Z0-9\\u4e00-\\u9fa5]{4,16}$", nickname_et.getText().toString())) {
//                    nickname_et.setError("昵称由4~16位字母、数字或汉字组成！");
//                    showToast("昵称由4~16位字母、数字或汉字组成！");
//                    nickname_et.requestFocus();
//                    nickname_et.setSelectAllOnFocus(true);
//                    return;
//                }

                requestChangeAlias();
            }
        });
    }

    /**
     *  修改昵称
     */
    private void requestChangeAlias() {
        String nickname = nickname_et.getText().toString();
        try {
            JSONObject request_json = new JSONObject();
            request_json.put("userno", MySharedPreferences.getInstance().getUserNo());
            request_json.put("alias", nickname);
            httpPostJson(CommandUtil.CHANGEALIAS, Commands.CHANGEALIAS, request_json, true);
        } catch (JSONException e) {

        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.CHANGEALIAS:
                MySharedPreferences.getInstance().setAlias(nickname_et.getText().toString());
                AppToast.getToast().show("昵称已更新");
                finish();
                break;
        }
    }
}
